<?php
class ControllerVoucherSaleorder extends Controller
{
	private $error = array();
	
	public function index()
	{
		$this->id='content';
		$this->insert();
	}
	
	public function insert()
	{
		$this->load->model("voucher/order");
		$this->load->model("voucher/ordertemp");
		
		$orderid = $this->model_voucher_ordertemp->insertdefault("EXC");
		$this->redirect('index.php?route=voucher/saleorder/update&orderid='.$orderid);
		//$this->redirect($this->url->http('voucher/saleorder/update'));
	}
	
	public function update() {
		$this->load->model("voucher/order");
		$this->load->model("voucher/orderdetail");
		$this->load->model("voucher/ordertemp");
		$this->load->model("voucher/orderdetailtemp");
		
		$orderid = $this->request->get['orderid'];
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) {
			$this->_saveordertemp();
			$ordertemp = $this->model_voucher_ordertemp->getItem($orderid);
			$this->model_voucher_order->saveorder($ordertemp);
			$this->saveVoucher($ordertemp);
			$orderdetails = $this->model_voucher_orderdetailtemp->getList(" AND orderid = '".$orderid."'");
			foreach($orderdetails as $item)
			{
				$this->model_voucher_orderdetail->saveorderdetail($item);
			}
			//$this->redirect('index.php?route=voucher/saleorder/update&orderid='.$orderid);
			if($this->request->post['print'] == "1")
			{
				$this->redirect('index.php?route=voucher/saleorder/update&orderid='.$orderid.'&print=1');
			}
			else
			{
				$this->redirect('index.php?route=voucher/saleorder/update&orderid='.$orderid);
			}
		}
    
    	$this->getForm();
  	}

	public function edit() {
		$this->load->model("voucher/order");
		$this->load->model("voucher/orderdetail");
		$this->load->model("voucher/ordertemp");
		$this->load->model("voucher/orderdetailtemp");
		
		$orderid = $this->request->get['orderid'];
    	$ordersaved = $this->model_voucher_order->getItem($orderid);
		if(count($ordersaved) > 0)
		{
			$this->model_voucher_ordertemp->saveorder($ordersaved);
			$this->model_voucher_orderdetailtemp->deleteorder($orderid);
			$orderdetails = $this->model_voucher_orderdetail->getList(" AND orderid = '".$orderid."'");
			foreach($orderdetails as $item)
			{
				$this->model_voucher_orderdetailtemp->saveorderdetail($item);
			}
		}
    	$this->redirect('index.php?route=voucher/saleorder/update&orderid='.$orderid);
  	}
	
	
	
	public function saveordertemp(){
		$this->_saveordertemp();
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
	}
	
	public function barcode(){
		$this->load->model("voucher/ordertemp");
		$this->load->model("voucher/orderdetailtemp");
		$this->load->model("category/product");
		
		$orderid = $this->request->get['orderid'];
		$barcode = $this->request->get['barcode'];
		
		$product = $this->model_category_product->getByBarcode($barcode);
		
		if($product['productid'] != "")
		{
			$data['orderid'] = $orderid;
			$data['orderdetailno'] = "";
			$data['productid'] = $product['productid'];
			$data['productname'] = $product['productname'];
			$data['stockid'] = "";
			$data['qty'] = 1;
			$data['unit'] = $product['unit'];
			$data['netprice'] = $product['netprice'];
			$data['saleprice'] = $product['saleprice'];
			$data['orderprice'] = $product['saleprice'];
			$data['subtotal'] = (float)$product['orderprice'];
			$data['tax'] = 0;
			$data['taxamount'] = 0;
			$data['grandtotal'] = (float)$product['orderprice'];
			$data['discount'] = 0;
			$data['discountamount'] = 0;
			$data['note'] = "";
			$data['expireddate'] = "";
			$data['warranty'] = "";
			
			$this->model_voucher_orderdetailtemp->insert($data);
			$this->data['output'] = "";
		}
		else
		{
			$this->data['output'] = "Not found";			
		}
		
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();

		
	}
	
	public function _saveordertemp(){
		$this->load->model("voucher/ordertemp");
		$this->load->model("voucher/orderdetailtemp");
		$this->load->model("category/product");
		
		if (($this->request->server['REQUEST_METHOD'] == 'POST') && ($this->validateForm())) {
			$this->request->post['orderid'] = $this->request->get['orderid'];
			$this->request->post['orderdate'] = $this->date->formatMySQLDateQuery($this->request->post['orderdate'],"DMY","/");
			$this->request->post['subtotal'] = $this->convertnumber($this->request->post['order_subtotal']);
			$this->request->post['taxamount'] = $this->convertnumber($this->request->post['order_taxamount']);
			$this->request->post['discountamount'] = $this->convertnumber($this->request->post['order_discountamount']);
			$this->request->post['grandtotal'] = $this->convertnumber($this->request->post['order_grandtotal']);
			$this->request->post['paymentamount'] = $this->convertnumber($this->request->post['order_paymentamount']);
			$this->request->post['deptamount'] = $this->convertnumber($this->request->post['order_deptamount']);
			
			$this->model_voucher_ordertemp->update($this->request->post);
			if (isset($this->request->post['orderdetailid'])) 
			{
				foreach ($this->request->post['orderdetailid'] as $orderdetailid) {
				
					$orderid = $this->request->post['orderid'];
					$product = $this->model_category_product->getItem($this->request->post['productid'][$orderdetailid]);
					
					$data['orderdetailid'] = $orderdetailid;
					$data['orderid'] = $orderid;
					$data['orderdetailno'] = "";
					$data['productid'] = $product['productid'];
					$data['productname'] = $product['productname'];
					$data['stockid'] = $this->request->post['stockid'][$orderdetailid];
					$data['qty'] = $this->convertnumber($this->request->post['qty'][$orderdetailid]);
					$data['exportqty'] = $data['qty'] * -1;
					$data['unit'] = $product['unit'];
					$data['saleprice'] = $product['saleprice'];
					$data['netprice'] = $product['netprice'];
					$data['orderprice'] = $this->convertnumber($this->request->post['orderprice'][$orderdetailid]);
					$data['subtotal'] = (float)$data['orderprice'] * (float)$data['qty'] ;
					$data['tax'] = $this->convertnumber($this->request->post['tax'][$orderdetailid]);
					$data['taxamount'] = round((float)$data['subtotal'] / 100 * (float)$data["tax"],2);
					$data['grandtotal'] = (float)$data['subtotal'] + (float)$data['taxamount']; 
					$data['discount'] = $this->convertnumber($this->request->post['discount'][$orderdetailid]);
					$data['discountamount'] = round((float)$data['grandtotal'] / 100 * (float)$data["discount"],2);
					$data['note'] = $this->request->post['note'][$orderdetailid];
					$data['expireddate'] = "";
					$data['warranty'] = "";
					
					$this->model_voucher_orderdetailtemp->update($data);
					//$this->data['output'] .= $this->request->post['productid'][$orderdetailid].",";		
				
				}
			}			
			
			$this->data['output'] = "<?php echo $this->document->text['button_ok']?>";
			//$this->data['output'] = count($this->request->post['orderdetailid']);
		}
		
		
   
	}
	
	private function convertnumber($str)
	{
		$str = str_replace("," ,"",$str);
		return (float)$str;
	}
	
	private function getForm()
	{
		$this->load->model("core/user");
		$this->load->model("common/control");
		$this->load->model("category/stock");
		$this->load->model("voucher/ordertemp");
		$this->load->model("voucher/orderdetailtemp");
		
		$this->data['orderid'] = $this->request->get['orderid'];
		
		$this->data['allowdelete'] = false;
		if($this->request->get['type'] == "edit")
		{
			$this->data['allowdelete'] = true;
		}
		
		
		$this->data['error'] = @$this->error;
		
		if (!isset($this->request->get['orderid'])) {
			$this->data['action'] = $this->url->http('voucher/saleorder/insert');
		} else {
			$this->data['action'] = $this->url->http('voucher/saleorder/update&orderid=' . $this->request->get['orderid']);
		}
		
		$this->data['cancel'] = $this->url->https('voucher/saleorder');
	
	
		
		$stocks = $this->model_category_stock->getList();
		
		
		
		if ((isset($this->request->get['orderid'])) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$this->data['order'] = $this->model_voucher_ordertemp->getItem($this->request->get['orderid']);
			$this->data['orderdetails'] = $this->model_voucher_orderdetailtemp->getList(" AND orderid = '".$this->request->get['orderid']."'");
			for($i=0; $i < count($this->data['orderdetails']); $i++)
			{
				$this->data['orderdetails'][$i]['cb_stocks'] = $this->model_common_control->getDataCombobox($stocks, "stockname", "stockid", ""); 
			}
    	}
		else
		{
			$this->data['order'] = $this->request->post;
		}
		
		//Set default values
		if($this->data['order']['ordernumber'] == "") {$this->data['order']['ordernumber'] = "auto";}
		if($this->data['order']['orderdate'] == "") {$this->data['order']['orderdate'] = date("Y-m-j H:m:s");}
		
		
		$paymenttypes = array(
			0 => array("paymenttype" => "cash"),
			1 => array("paymenttype" => "bank")
		);
		
		$suppliers = $this->model_core_user->getList("AND usergroup = 'customer'");
		$users = $this->model_core_user->getList("AND usergroup = ''");
		
		$this->data['cb_suppliers'] = $this->model_common_control->getDataCombobox($suppliers, "fullname", "userid", $this->data['order']['partnerid']);
		$this->data['cb_users'] = $this->model_common_control->getDataCombobox($users, "fullname", "userid", $this->data['order']['employeeid']);
		
		$this->data['cb_paymenttypes'] = $this->model_common_control->getDataCombobox($paymenttypes, "paymenttype", "paymenttype", $this->data['order']['paymenttype']);
		
		$this->id='content';
		$this->template='voucher/saleorder.tpl';
		$this->layout="layout/function";
		
		$this->render();
	}
	
	public function delete() {
		//$this->load->language('core/site');
		//$this->data = array_merge($this->data, $this->language->getData());
		
		//$this->document->title = $this->language->get('heading_title');
		$this->load->model("voucher/orderdetailtemp");
			
    	if (isset($this->request->post['item'])) 
		{
			foreach ($this->request->post['item'] as $orderdetailid) {
				$this->model_voucher_orderdetailtemp->delete($orderdetailid);
			}
			$this->data['output'] = "Delete successfully";
    	}
	
    	//$this->getList();
		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
	
	private function validateForm()
	{	
		return TRUE;
		
		/*if (strlen($this->request->post['stockname']) == 0) {
      		$this->error['stockname'] = "Stock name cannot be null";
    	}

		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}*/
	}
	
	public function saveVoucher($order)
	{
		$this->load->model("voucher/voucher");
		$voucher = $this->model_voucher_voucher->getItemByOrderId($order['orderid']);
		$data['voucherid'] = $voucher['voucherid'];
		
		
		$data['voucherdate'] = $order['orderdate'];
		$data['totalpaid'] = $order['paymentamount'];
		$data['deptamount'] = $order['deptamount'];
		$data['grandtotal'] = $order['grandtotal'];
		
		$data['partnerid']=$order['partnerid'];
		$data['discount']=$order['discountamount'];
		$data['employeeid']=$order['employeeid'];
		
		$data['orderid']=$order['orderid'];
		$data['ordernumber']=$order['ordernumber'];
		$data['paymenttype']=$order['paymenttype'];
		
		
		$data['vouchergroup'] = "VINCS";
		$data['voucherno'] = $this->model_voucher_voucher->nextVoucherNo($data['vouchergroup']."_");
		$data['description'] = "Receipts from Sale order no #".$order['orderid'];
		$data['vouchertype']="INCOME";
		
		$voucherid = $this->model_voucher_voucher->save($data);
		return $voucherid;
	}
}
?>